package com.openbravo.pos.panels;

import javax.swing.JPanel;
import java.awt.FlowLayout;
import javax.swing.JButton;
import net.sourceforge.jtimepiece.TimeUtil;

import com.openbravo.beans.JCalendarDialog;
import com.openbravo.data.basic.BasicException;
import com.openbravo.format.Formats;
import com.openbravo.pos.base.AppLocal;
import com.openbravo.pos.panels.event.TimeCycleChangedEvent;
import com.openbravo.pos.panels.event.TimeCycleChangedListener;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Date;
import javax.swing.JToggleButton;
import javax.swing.ButtonGroup;
import java.awt.GridLayout;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.ImageIcon;

public class QuickTimerHFilter extends JPanel {
	 public static int TIMERCYCLETYPE_Null=-1;
     public static int TIMERCYCLETYPE_Today=0;
     public static int TIMERCYCLETYPE_Week=1;
     public static int TIMERCYCLETYPE_ThreeWeek=2;
     public static int TIMERCYCLETYPE_Month=3;
     public static int TIMERCYCLETYPE_Muarter=4;
     
     private int timeCycleType;
     private java.util.Date startDate;
     private java.util.Date endDate;
	/**
	 * Create the panel.
	 */
	private JToggleButton btnMuarter;
	private final ButtonGroup buttonGroup = new ButtonGroup();
	private JToggleButton bntToday;
	private JToggleButton btnWeek;
	private JToggleButton btnThreeWeek;
	private JToggleButton btnMonth;
	private JButton btnLastCycle;
	private JButton btnNextCycle;
	private JPanel panel;
	private JLabel label;
	private JTextField jTxtStartDate;
	private JButton button;
	private JPanel panel_1;
	private JTextField jTxtEndDate;
	private JButton button_1;
	public QuickTimerHFilter() {
		initComponents();
		timeCycleType=TIMERCYCLETYPE_Null;
	}
	
	private void initComponents() {
		
		 bntToday = new JToggleButton(AppLocal.getIntString("Button.Today"));
		 bntToday.addActionListener(new ActionListener() {
		 	public void actionPerformed(ActionEvent e) {
		 		fireTodayCycle();
		 	}
		 });
		setLayout(new GridLayout(10, 1, 0, 0));
		buttonGroup.add(bntToday);
		add(bntToday);
		
		 btnWeek = new JToggleButton(AppLocal.getIntString("Button.CurrentWeek"));
		 btnWeek.addActionListener(new ActionListener() {
		 	public void actionPerformed(ActionEvent e) {
		 		fireWeekCycle();
		 	}
		 });
		buttonGroup.add(btnWeek);
		add(btnWeek);
		
		 btnThreeWeek = new JToggleButton(AppLocal.getIntString("Button.CurrentThreeWeek"));
		 btnThreeWeek.addActionListener(new ActionListener() {
		 	public void actionPerformed(ActionEvent e) {
		 		fireThreeWeekCycle();
		 	}
		 });
		buttonGroup.add(btnThreeWeek);
		add(btnThreeWeek);
		
		 btnMonth = new JToggleButton(AppLocal.getIntString("Button.CurrentMonth"));
		 btnMonth.addActionListener(new ActionListener() {
		 	public void actionPerformed(ActionEvent e) {
		 		fireMonthCycle();
		 	}
		 });
		buttonGroup.add(btnMonth);
		add(btnMonth);
		
		btnMuarter = new JToggleButton(AppLocal.getIntString("Button.CurrentMuarter"));
		btnMuarter.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				fireMuarterCycle();
			}
		});
		buttonGroup.add(btnMuarter);
		add(btnMuarter);
		
		 btnLastCycle = new JButton(AppLocal.getIntString("Button.LastCycle"));
		 btnLastCycle.addActionListener(new ActionListener() {
		 	public void actionPerformed(ActionEvent e) {
		 		fireLastCycle();
		 	}
		 });
		add(btnLastCycle);
		
		 btnNextCycle = new JButton(AppLocal.getIntString("Button.NextCycle"));
		 btnNextCycle.addActionListener(new ActionListener() {
		 	public void actionPerformed(ActionEvent e) {
		 		fireNextCycle();
		 	}
		 });
		add(btnNextCycle);
		
		panel = new JPanel();
		add(panel);
		
		label = new JLabel("启");
		panel.add(label);
		
		jTxtStartDate = new JTextField();
		panel.add(jTxtStartDate);
		jTxtStartDate.setColumns(16);
		
		button = new JButton("");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				btnDateStartActionPerformed(e);
			}
		});
		button.setIcon(new ImageIcon(QuickTimerHFilter.class.getResource("/com/openbravo/images/1day.png")));
		panel.add(button);
		
		panel_1 = new JPanel();
		add(panel_1);
		panel_1.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		
		JLabel label_1 = new JLabel("止");
		panel_1.add(label_1);
		
		jTxtEndDate = new JTextField();
		panel_1.add(jTxtEndDate);
		jTxtEndDate.setColumns(16);
		
		JButton btnNewButton = new JButton("");
		btnNewButton.setIcon(new ImageIcon(QuickTimerHFilter.class.getResource("/com/openbravo/images/1day.png")));
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				btnDateEndActionPerformed(e);
			}
		});
		panel_1.add(btnNewButton);
		
		button_1 = new JButton("自定义");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				fireCustomerCycle();
			}
		});
		add(button_1);
	}
	
    protected void fireCustomerCycle() {
    	
		try {
			startDate = (Date) Formats.TIMESTAMP.parseValue(jTxtStartDate.getText());
		} catch (BasicException e) {
			startDate = null;
		}
		try {
			endDate = (Date) Formats.TIMESTAMP.parseValue(jTxtEndDate.getText());
		} catch (BasicException e) {
			endDate = null;
		}
		if(startDate==null&&endDate==null)
			return;
		timeCycleType=TIMERCYCLETYPE_Null;
		
		fireTimeCycleChanged();
		
	}

	public void addTimeCycleChangedListener(TimeCycleChangedListener l){
    	listenerList.add(TimeCycleChangedListener.class, l);
    }
    
    public void removeTimeCycleChangedListener(TimeCycleChangedListener l){
    	listenerList.remove(TimeCycleChangedListener.class, l);
    }
    
    public void fireTimeCycleChanged(){
		jTxtStartDate.setText(startDate != null?Formats.TIMESTAMP.formatValue(startDate):null);
		jTxtEndDate.setText(endDate != null?Formats.TIMESTAMP.formatValue(endDate):null);
    	for(TimeCycleChangedListener l:listenerList.getListeners(TimeCycleChangedListener.class))
    		l.timeCycleChanged(new TimeCycleChangedEvent(this,timeCycleType,startDate, endDate));
    }

	public int getTimeCycleType() {
		return timeCycleType;
	}

	public void setTimeCycleType(int timeCycleType) {
		this.timeCycleType = timeCycleType;
	}

	public java.util.Date getStartDate() {
		return startDate;
	}

	public void setStartDate(java.util.Date startDate) {
		this.startDate = startDate;
	}

	public java.util.Date getEndDate() {
		return endDate;
	}

	public void setEndDate(java.util.Date endDate) {
		this.endDate = endDate;
	}
	
	public void resert(){
		timeCycleType=TIMERCYCLETYPE_Null;
		startDate=null;
		endDate=null;
		buttonGroup.clearSelection();
	}

	public void setDefineSelected(int type){
		if(type==TIMERCYCLETYPE_Null){
			resert();
		}
		if(type== TIMERCYCLETYPE_Today){
			  bntToday.setSelected(true);
			  fireTodayCycle();
		}if(type== TIMERCYCLETYPE_Week){
			  btnWeek.setSelected(true);
			  fireWeekCycle();
		}if(type== TIMERCYCLETYPE_ThreeWeek){
			  btnThreeWeek.setSelected(true);
			  fireThreeWeekCycle();
		}if(type==TIMERCYCLETYPE_Month){
		      btnMonth.setSelected(true);
		      fireMonthCycle();
		}if(type==TIMERCYCLETYPE_Muarter){
			  btnMuarter.setSelected(true);
			  fireMuarterCycle();
		}
	}
	
	public void fireTodayCycle() {
		startDate=TimeUtil.GetDateNoTime(new Date());
	    this.endDate=null;	
		timeCycleType=QuickTimerFilter.TIMERCYCLETYPE_Today;
		fireTimeCycleChanged();
	}


	public void fireWeekCycle() {
		startDate=TimeUtil.GetDateNoTime(new Date());
		startDate=TimeUtil.getFirstDateForWeekOfDate(startDate);
	    endDate=null;
		timeCycleType=QuickTimerFilter.TIMERCYCLETYPE_Week;
		fireTimeCycleChanged();
	}

	
	public void fireThreeWeekCycle() {
		
		startDate=TimeUtil.GetDateNoTime(new Date());	
		startDate=TimeUtil.getFirstDateForWeekOfDate(startDate);
		startDate=TimeUtil.addDay(startDate, -14);
		endDate=null;
		timeCycleType=QuickTimerFilter.TIMERCYCLETYPE_ThreeWeek;
		fireTimeCycleChanged();
	}


	public void fireMonthCycle() {
	    startDate=TimeUtil.GetDateNoTime(new Date());	
		startDate=TimeUtil.getFirstDayForMonthOfDate(startDate);
		endDate=null;	
		timeCycleType=QuickTimerFilter.TIMERCYCLETYPE_Month;
		fireTimeCycleChanged();
	}


	public void fireMuarterCycle() {
		
	}


	public void fireLastCycle() {
		if(startDate==null&&endDate==null)
			return;
		if(timeCycleType==QuickTimerFilter.TIMERCYCLETYPE_Null){	
			if(startDate!=null&&endDate!=null){
				int days=TimeUtil.getCalendarDaysAfter((Date)startDate, (Date)endDate);
				startDate=TimeUtil.addDay((Date)startDate, -days);
				endDate=TimeUtil.addDay((Date)endDate, -days);
			}else if(startDate!=null){
				int days=TimeUtil.getCalendarDaysAfter((Date)startDate, new Date());
				endDate=startDate;
				startDate=TimeUtil.addDay((Date)startDate, -days);
			}
		}else if(timeCycleType==QuickTimerFilter.TIMERCYCLETYPE_Today){
			int days=1;
			endDate=startDate;
			startDate=TimeUtil.addDay((Date)startDate, -days);
		}else if(timeCycleType==QuickTimerFilter.TIMERCYCLETYPE_Week){
			int days=7;
			endDate=startDate;
			startDate=TimeUtil.addDay((Date)startDate, -days);   
		}else if(timeCycleType==QuickTimerFilter.TIMERCYCLETYPE_ThreeWeek){
			int days=21;
			endDate=startDate;
			startDate=TimeUtil.addDay((Date)startDate, -days);   
		}else if(timeCycleType==QuickTimerFilter.TIMERCYCLETYPE_Month){
			endDate=startDate;
			startDate=TimeUtil.addMonth((Date)startDate, -1);
		}
		fireTimeCycleChanged();
	}

	public void fireNextCycle() {
		if(startDate==null&&endDate==null)
			return;
		if(startDate==null)
			startDate=TimeUtil.GetDateNoTime(new Date());
		if(endDate == null)
			endDate = startDate;
	    if(timeCycleType==QuickTimerFilter.TIMERCYCLETYPE_Null){	
	    	if(startDate!=null&&endDate!=null){
				int days=TimeUtil.getCalendarDaysAfter((Date)startDate, (Date)endDate);
				startDate=TimeUtil.addDay((Date)startDate, days);
				endDate=TimeUtil.addDay((Date)endDate, days);
			}else if(endDate!=null){
				int days=TimeUtil.getCalendarDaysAfter((Date)endDate, new Date());
				startDate=endDate;
				endDate=TimeUtil.addDay((Date)endDate, days);
			}
		}else if(timeCycleType==QuickTimerFilter.TIMERCYCLETYPE_Today){
			int days=1;
			startDate=endDate;
			endDate=TimeUtil.addDay((Date)endDate, days);
		}else if(timeCycleType==QuickTimerFilter.TIMERCYCLETYPE_Week){
			int days=7;
			startDate=endDate;
			endDate=TimeUtil.addDay((Date)endDate, days);   
		}else if(timeCycleType==QuickTimerFilter.TIMERCYCLETYPE_ThreeWeek){
			int days=21;
			startDate=endDate;
			endDate=TimeUtil.addDay((Date)endDate, days);   
		}else if(timeCycleType==QuickTimerFilter.TIMERCYCLETYPE_Month){
			startDate=endDate;
			endDate=TimeUtil.addMonth((Date)endDate, 1);
		}  
	    fireTimeCycleChanged();
	}
	
	
	private void btnDateStartActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_btnDateStartActionPerformed
		Date date;
		try {
			date = (Date) Formats.TIMESTAMP.parseValue(jTxtStartDate.getText());
		} catch (BasicException e) {
			date = null;
		}
		date = JCalendarDialog.showCalendarTimeHours(this, date);
		if (date != null) {
			jTxtStartDate.setText(Formats.TIMESTAMP.formatValue(date));
		}
	}// GEN-LAST:event_btnDateStartActionPerformed

	private void btnDateEndActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_btnDateEndActionPerformed
		Date date;
		try {
			date = (Date) Formats.TIMESTAMP.parseValue(jTxtEndDate.getText());
		} catch (BasicException e) {
			date = null;
		}
		date = JCalendarDialog.showCalendarTimeHours(this, date);
		if (date != null) {
			jTxtEndDate.setText(Formats.TIMESTAMP.formatValue(date));
		}
	}// GEN-LAST:event_btnDateEndActionPerformed

}

